package person.bangbang.im.Androidgin.Framework;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class Buddy {
	/* 
	 * format [protocolId]:[idInThisProtocol]
	 * e.g fetion:123456788@fetion.cn
	 */
	private String mId;
	
	private String mUserName;
	private String mAlias;

	private String mImpresa = null;
	private StatusType mType = new StatusType(StatusType.ONLINE, "online");

	private List<OnBuddyEvent> mOnBuddyListener = new ArrayList<OnBuddyEvent>();
	public String getMImpresa() {
		return mImpresa;
	}

	public void setMImpresa(String impresa) {
		mImpresa = impresa;
	}

	/**
	 * 好友可按： 1）账户自己定义格式分类； 2) Androidgin 自己定义格式分类
	 * 
	 * 因此，保留一个指向自己所属账户的引用。
	 */
	private Account account;

	private Buddy() {

	}

	public Buddy(String id, String userName, Account acc) {
		mId = id;
		mUserName = userName;
		account = acc;
	}

	public String getID() {
		return mId;
	}

	public String getUserName() {
		return mUserName;
	}

	public Account getAccount() {
		return account;
	}

	public void setUserName(String userName) {
		mUserName = userName;
	}

	public void setAlias(String alias) {
		mAlias = alias;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public String toString() {
		return "Buddy:" + hashCode() + '[' + "protocol="
				+ account.getProtocolID() + "userName=" + mUserName + "userId="
				+ mId + ']';
	}
	
	public void regOnBuddyListener(OnBuddyEvent l) {
		mOnBuddyListener.add(l);
	}
	public void unRegOnBuddyListener(OnBuddyEvent l) {
		mOnBuddyListener.remove(l);
	}
	public void doSignedOn() {
		for (OnBuddyEvent l : mOnBuddyListener) {
			l.onSignedOn(this);
		}
	}
	public void doSignedOff() {
		for (OnBuddyEvent l : mOnBuddyListener) {
			l.onSignedOff(this);
		}
	}
	public void doAway() {
		for (OnBuddyEvent l : mOnBuddyListener) {
			l.onAway(this);
		}
	}
	public void doIdleChanged(boolean oldIdle, boolean idle) {
		for (OnBuddyEvent l : mOnBuddyListener) {
			l.onIdleChanged(this, oldIdle, idle);
		}
	}
	public void doSignedOff(boolean oldIdle, boolean idle) {
		for (OnBuddyEvent l : mOnBuddyListener) {
			l.onSignedOff(this);
		}
	}
	public interface OnBuddyEvent {
		/**
		 * Emitted when a buddy on your buddy list goes away. 
		 * 
		 * @param b
		 */
		public void onAway(Buddy b);
		
		/**
		 * Emitted when a buddy on your buddy list becomes idle. 
		 * 
		 * @param b
		 * @param oldIdle
		 * @param idle
		 * @return
		 */
		public void onIdleChanged(Buddy b, boolean oldIdle, boolean idle);
		/**
		 * Emitted when a buddy on your buddy list signs on
		 * @param b
		 */
		public void onSignedOn(Buddy b);
		/**
		 * Emitted when a buddy on your buddy list signs off.
		 * 
		 * @param b
		 */
		public void onSignedOff(Buddy b);

	}
}
